<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Shiro权限管理系统</title>
    <link href="/assets/images/favicon.jpg" rel="shortcut icon" type="image/x-icon">
    <link href="https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <link href="https://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.bootcss.com/jquery-confirm/2.5.1/jquery-confirm.min.css" rel="stylesheet">
    <link href="https://cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.css" rel="stylesheet">
    <link href="https://cdn.bootcss.com/nprogress/0.2.0/nprogress.min.css" rel="stylesheet">
    <link href="https://cdn.bootcss.com/toastr.js/2.0.3/css/toastr.min.css" rel="stylesheet">
    <link href="https://cdn.bootcss.com/iCheck/1.0.2/skins/square/green.css" rel="stylesheet">
    <link href="https://cdn.bootcss.com/bootstrap-table/1.11.1/bootstrap-table.min.css" rel="stylesheet">
    <link href="https://cdn.bootcss.com/bootstrap-daterangepicker/2.1.24/daterangepicker.min.css" rel="stylesheet">
    <link href="https://cdn.bootcss.com/bootstrap-datetimepicker/4.17.47/css/bootstrap-datetimepicker.min.css"
          rel="stylesheet">
    <link href="https://cdn.bootcss.com/zTree.v3/3.5.29/css/metroStyle/metroStyle.min.css" rel="stylesheet">

    <link href="/assets/css/zhyd.core.css" rel="stylesheet">
</head>
<body class="nav-md">
<div class="container body">
    <div class="main_container">
        <div class="col-md-3 left_col">
            <div class="left_col scroll-view">
                <div class="navbar nav_title" style="border: 0;">
                    <a href="/" class="site_title"><i class="fa fa-coffee"></i> <span>Shiro权限管理系统</span></a>
                </div>
                <div class="clearfix"></div>
                <div class="profile clearfix">
                    <div class="profile_pic">
                        <img src="/assets/images/loading.gif" alt="..." class="img-circle profile_img">
                    </div>
                    <div class="profile_info">
                        <span>早上好,</span>
                        <h2>尊敬的管理员</h2>
                    </div>
                </div>
                <br/>
                <!-- sidebar menu -->
                <div id="sidebar-menu" class="main_menu_side hidden-print main_menu">
                    <div class="menu_section">
                        <ul class="nav side-menu">
                            <li><a href="/index"><i class="fa fa-home"></i>首页</a></li>
                            <li>
                                <a><i class="fa fa-users"></i> 用户管理<span class="fa fa-chevron-down"></span></a>
                                <ul class="nav child_menu">
                                    <li><a href="/users"><i class=""></i>用户列表</a></li>
                                </ul>
                            </li>
                            <li>
                                <a><i class="fa fa-cogs"></i> 系统配置<span class="fa fa-chevron-down"></span></a>
                                <ul class="nav child_menu">
                                    <li><a href="/resources"><i class=""></i>资源管理</a></li>
                                    <li><a href="/roles"><i class=""></i>角色管理</a></li>
                                </ul>
                            </li>
                            <li>
                                <a><i class="fa fa-heartbeat"></i> 数据监控<span class="fa fa-chevron-down"></span></a>
                                <ul class="nav child_menu">
                                    <li><a href="/druid/index.html" target="_blank"><i class=""></i>Druid监控</a></li>
                                </ul>
                            </li>
                        </ul>
                    </div>
                </div>
                <div class="sidebar-footer hidden-small">
                    <a>&nbsp;</a>
                    <a>&nbsp;</a>
                    <a>&nbsp;</a>
                    <a href="/passport/logout" data-toggle="tooltip" data-placement="top" title=""
                       data-original-title="退出系统">
                        <span class="glyphicon glyphicon-off" aria-hidden="true"></span>
                    </a>
                </div>
            </div>
        </div>
        <div class="top_nav">
            <div class="nav_menu">
                <nav>
                    <div class="nav toggle">
                        <a id="menu_toggle"><i class="fa fa-bars"></i></a>
                    </div>
                    <ul class="nav navbar-nav navbar-right">
                        <li class="">
                            <a href="javascript:;" class="user-profile dropdown-toggle" data-toggle="dropdown"
                               aria-expanded="false">
                                <img src="/assets/images/loading.gif" alt=""><span th:if="session.user != null">[[${session.user.username}]]</span><span th:if="session.user == null">admin</span>
                                <span class=" fa fa-angle-down"></span>
                            </a>
                            <ul class="dropdown-menu dropdown-usermenu pull-right">
                                <li><a href="/passport/logout"><i class="fa fa-sign-out pull-right"></i> 退出系统</a></li>
                            </ul>
                        </li>
                    </ul>
                </nav>
            </div>
        </div>
        <div class="right_col" role="main">
            <div class="clearfix"></div>
            <div class="row">
                <div class="col-md-12 col-sm-12 col-xs-12">
                    <ol class="breadcrumb">
                        <li><a href="/">首页</a></li>
                        <li class="active">用户管理</li>
                    </ol>
                    <div class="x_panel">
                        <div class="x_content">
                            <div class="">
                                <div class="btn-group hidden-xs" id="toolbar">
                                    <shiro:hasPermission name="role:add">
                                        <button id="btn_add" type="button" class="btn btn-default" title="新增用户">
                                            <i class="fa fa-plus"></i> 新增角色
                                        </button>
                                    </shiro:hasPermission>
                                    <shiro:hasPermission name="role:batchDelete">
                                        <button id="btn_delete_ids" type="button" class="btn btn-default" title="删除选中">
                                            <i class="fa fa-trash-o"></i> 批量删除
                                        </button>
                                    </shiro:hasPermission>
                                </div>
                                <table id="tablelist">
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <footer>
                <div class="pull-right">
                    Copyright © 2018 <a href="https://www.zhyd.me" target="_blank">yadong.zhang</a> · Powered by <a
                        href="https://gitee.com/yadong.zhang/DBlog" title="DBlog是一款简洁美观、自适应的Java博客系统..."
                        target="_blank"><strong>DBlog</strong></a>.
                    All Rights Reserved. Current version is 1.0.
                </div>
                <div class="clearfix"></div>
            </footer>
        </div>
    </div>
    <script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js" type="text/javascript"></script>
    <script src="http://cdn.bootcss.com/bootstrap/3.3.0/js/bootstrap.min.js" type="text/javascript"></script>
    <script src="https://cdn.bootcss.com/jquery-confirm/2.5.1/jquery-confirm.min.js" type="text/javascript"></script>
    <script src="https://cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.js" type="text/javascript"></script>
    <script src="https://cdn.bootcss.com/mustache.js/2.3.0/mustache.min.js" type="text/javascript"></script>
    <script src="https://cdn.bootcss.com/js-xss/0.3.3/xss.min.js" type="text/javascript"></script>
    <script src="https://cdn.bootcss.com/nprogress/0.2.0/nprogress.min.js"></script>
    <script src="https://cdn.bootcss.com/toastr.js/2.0.3/js/toastr.min.js"></script>
    <script src="https://cdn.bootcss.com/iCheck/1.0.2/icheck.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap-table/1.11.1/bootstrap-table.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap-table/1.11.1/locale/bootstrap-table-zh-CN.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap-daterangepicker/2.1.24/moment.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap-daterangepicker/2.1.24/daterangepicker.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap-datetimepicker/4.17.47/js/bootstrap-datetimepicker.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap-progressbar/0.9.0/bootstrap-progressbar.min.js"></script>
    <script src="https://cdn.bootcss.com/zTree.v3/3.5.29/js/jquery.ztree.core.min.js"></script>
    <script src="https://cdn.bootcss.com/zTree.v3/3.5.29/js/jquery.ztree.excheck.min.js"></script>

    <script src="/assets/js/validator.js"></script>
    <script src="/assets/js/jquery-form.js"></script>
    <script src="/assets/js/zyd.tool.js"></script>
    <script src="/assets/js/zhyd.core.js"></script>
    <script src="/assets/js/zyd.table.js"></script>
</body>
</html>
<!--弹框-->
<div class="modal fade bs-example-modal-sm" id="selectRole" tabindex="-1" role="dialog"
     aria-labelledby="selectRoleLabel">
    <div class="modal-dialog modal-sm" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="selectRoleLabel">分配资源权限</h4>
            </div>
            <div class="modal-body">
                <form id="boxRoleForm">
                    <div class="zTreeDemoBackground left">
                        <ul id="treeDemo" class="ztree"></ul>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>
<!--/弹框-->
<!--添加角色弹框-->
<div class="modal fade" id="addOrUpdateModal" tabindex="-1" role="dialog" aria-labelledby="addroleLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="addroleLabel">添加角色</h4>
            </div>
            <div class="modal-body">
                <form id="addOrUpdateForm" class="form-horizontal form-label-left" novalidate>
                    <input type="hidden" name="id">
                    <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="description">角色名称: <span
                                class="required">*</span></label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                            <input type="text" class="form-control col-md-7 col-xs-12" name="description"
                                   id="description" required="required" placeholder="请输入角色名称"/>
                        </div>
                    </div>
                    <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="available">是否可用: <span
                                class="required">*</span></label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                            <select name="available" id="available" required="required"
                                    class="form-control col-md-7 col-xs-12">
                                <option value="">请选择</option>
                                <option value="0">不可用</option>
                                <option value="1" selected="selected">可用</option>
                            </select>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary addOrUpdateBtn">保存</button>
            </div>
        </div>
    </div>
</div>
<!--/添加角色弹框-->
<script>
    /**
     * 操作按钮
     * @param code
     * @param row
     * @param index
     * @returns {string}
     */
    function operateFormatter(code, row, index) {
        var trId = row.id;
        var operateBtn = [
            '<shiro:hasPermission name="role:edit"><a class="btn btn-xs btn-primary btn-update" data-id="' + trId + '"><i class="fa fa-edit"></i>编辑</a></shiro:hasPermission>',
            '<shiro:hasPermission name="role:delete"><a class="btn btn-xs btn-danger btn-remove" data-id="' + trId + '"><i class="fa fa-trash-o"></i>删除</a></shiro:hasPermission>',
            '<shiro:hasPermission name="role:allotResource"><a class="btn btn-xs btn-info btn-allot" data-id="' + trId + '"><i class="fa fa-circle-thin"></i>分配资源</a></shiro:hasPermission>'
        ];

        // var operateBtn = [
        //     '<a class="btn btn-xs btn-primary btn-update" data-id=' + trId + '><i class="fa fa-edit"></i>编辑</a>',
        // ];
        // operateBtn.push('<a class="btn btn-xs btn-danger btn-remove" data-id=' + trId + '><i class="fa fa-trash-o"></i>删除</a>');
        // operateBtn.push('<a class="btn btn-xs btn-info btn-allot" data-id=' + trId + '><i class="fa fa-circle-thin"></i>分配资源</a>');

        console.log(operateBtn);

        return operateBtn.join('');
    }

    $(function () {
        var options = {
            url: "/roles/list",
            getInfoUrl: "/roles/get/{id}",
            updateUrl: "/roles/edit",
            removeUrl: "/roles/remove",
            createUrl: "/roles/add",
            saveRolesUrl: "/roles/saveRoleResources",
            columns: [{
                checkbox: true
            }, {
                field: 'name',
                title: '角色名',
                editable: false,
            }, {
                field: 'description',
                title: '角色描述',
                editable: false,
            }, {
                field: 'available',
                title: '是否可用',
                editable: true,
                formatter: function (code) {
                    return code ? '可用' : '不可用';
                }
            }, {
                field: 'operate',
                title: '操作',
                formatter: operateFormatter //自定义方法，添加操作按钮
            }],
            modalName: "角色"
        };
        //1.初始化Table
        $.tableUtil.init(options);
        //2.初始化Button的点击事件
        $.buttonUtil.init(options);

        /* 分配资源权限 */
        $('#tablelist').on('click', '.btn-allot', function () {
            console.log("分配资源权限");
            var $this = $(this);
            var rolesId = $this.attr("data-id");
            $.ajax({
                async: false,
                type: "POST",
                data: {rid: rolesId},
                url: '/resources/resourcesWithSelected',
                dataType: 'json',
                success: function (json) {
                    var data = json.data;
                    console.log(data);
                    var setting = {
                        check: {
                            enable: true,
                            chkboxType: {"Y": "ps", "N": "ps"},
                            chkStyle: "checkbox"
                        },
                        data: {
                            simpleData: {
                                enable: true
                            }
                        },
                        callback: {
                            onCheck: function (event, treeId, treeNode) {
                                console.log(treeNode.tId + ", " + treeNode.name + "," + treeNode.checked);
                                var treeObj = $.fn.zTree.getZTreeObj(treeId);
                                var nodes = treeObj.getCheckedNodes(true);
                                var ids = new Array();
                                for (var i = 0; i < nodes.length; i++) {
                                    //获取选中节点的值
                                    ids.push(nodes[i].id);
                                }
                                console.log(ids);
                                console.log(rolesId);
                                $.post(options.saveRolesUrl, {
                                    "roleId": rolesId,
                                    "resourcesId": ids.join(",")
                                }, function (obj) {
                                }, 'json');
                            }
                        }
                    };
                    var tree = $.fn.zTree.init($("#treeDemo"), setting, data);
                    tree.expandAll(true);//全部展开

                    $('#selectRole').modal('show');
                }
            });
        });
    });
</script>